package com.hjy.springbootinit.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @version 1.0
 * @Author HJY_Maker
 * @Date 2024/5/18 星期六 11:09
 */
@Configuration
//指向yml的配置文件中的spring.redis配置
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {
    private int database;
    private String host;
    private int port;

    //如果redis默认没有密码，则不用写
    private String password;
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                //设置数据库
                .setDatabase(database)
//                .setDatabase(1)
//                .setAddress("redis://" + "127.0.0.1" + ":" + "6379");
                //设置redis地址
                .setAddress("redis://" + host + ":" + port);
//        .setAddress("redis://" + "127.0.0.1" + ":" + "6379");
        //设置redis密码
//                .setPassword(password);
        //返回一个Redisson实例。
        return Redisson.create(config);
    }

}
